const express = require('express');
const axios = require('axios');
const app = express();

const cors = require('cors');
app.use(cors());
// 允许所有来源跨域访问
app.use((req, res, next) => {
  res.header("Access-Control-Allow-Origin", "*");
  next();
});

app.get('/api/netease_search', async (req, res) => {
  const { s } = req.query;
  try {
    const result = await axios.post(
      'https://music.163.com/api/search/get',
      `s=${encodeURIComponent(s)}&type=1&offset=0&limit=15`,
      { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }
    );
    res.json(result.data);
  } catch (e) {
    res.status(500).json({ error: '网易云请求失败' });
  }
});

app.get('/api/netease_search/lyric', async (req, res) => {
  const { id } = req.query;
  try {
    const result = await axios.get(
      `https://music.163.com/api/song/media?id=${encodeURIComponent(id)}`
    );
    res.json(result.data);
  } catch (e) {
    res.status(500).json({ error: '网易云请求失败' });
  }
});

app.listen(3000, () => console.log('代理服务器已启动'));